#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H

#include <QObject>

#include <mosquitto.h>
#include <string>
#include <iostream>
#include <string>
#include <cstring>
#include <thread>
#include "../control/keybordcontrol/KeyBoardManger.h"
#include "../player/images/ImageReceiver.h"
//结构体发送的数据类型
struct MqttMsg{
    std::string robotSn;
    std::string robotIp;//自定义其他的参数
};
class CMqttClient : public QObject
{
Q_OBJECT
public:
    explicit CMqttClient(QObject *parent = nullptr);
    ~CMqttClient();

    void Connect(const char* broker, int port, const char* username, const char* password);
    void Subscribe(const char* topic);
    void UnSubscribe(const char* topic);
    void Publish(const char* topic, const char* message);
    void init();
    void processMsg(const struct mosquitto_message* message);

    /*
     *@brief: 订阅cmd话题
     * @param[in]:topic
     */
    void SetCmdTopic(const QString &topic);
    /*
    *@brief: 发送Img请求Topic
    * @param[in]:topic
    */
    void SendImgRequset(const QString &topic, const QString &message);
    /*
    *@brief: 订阅cmd话题
    * @param[in]:topic
    */
    void SendImagesDataTopic(const QString &topic,  bool  flag);
signals:
    //设置
    void SendBasicDatas(const QString & msg);
private:
    struct mosquitto* mosq;
    QString m_broker;
    QString m_userName;
    QString m_password;
    int m_port;
    QVector<QString> subscribedTopics; // 存储所有已订阅的主题

    void setupCallbacks();
    void reconnectAndResubscribe();
    static void connectCallback(struct mosquitto *mosq, void *obj, int reason_code);
    static void messageCallback(struct mosquitto *mosq, void *obj, const struct mosquitto_message* message);
    void onSendMsg(const QString  &msg);

    std::string m_PubTopic1;  //发布topic
    std::string m_SubTopic1;  //订阅topic
    std::string m_ImagesDataTopic; //订阅对应导盲犬图片数据
    std::string m_dogSn;   //导盲犬的Sn
    std::string m_dogIp; // 导盲犬IP
    std::thread m_mqttThread; //发送数据的线程
    KeyBoardManger *m_keyManager; //接盘按键信心
    std::shared_ptr<CImageReceiver> m_pReciever; //接收数据
};

#endif // MQTTCLIENT_H
